#include <mpi.h>
#include <stdio.h>
#define N 1000000

int main(int argc, char* argv[]) 
{
	double local, pi, w;
	long i; 
	int taskid, numtask;
	w = 1.0 / N;
	
	MPI_Init(&argc, &argv);
	MPI_Comm_rank(MPI_COMM_WORLD, &taskid);
	MPI_Comm_size(MPI_COMM_WORLD, &numtask);
	
	for (i = taskid; i < N; i = i + numtask) 
	{
		local = (i + 0.5) * w;
		local = 4.0 / (1.0 + local * local);
	}
	
	MPI_Reduce( &local, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
	
	if (taskid == 0) 
	{
		printf("pi is %f\n", pi * w);
	}
	
	MPI_Finalize();
} //end main